/**
 * Copyright 2015 jianglibo@gmail.com
 *
 */
package com.m3958.logcollector.passwordvalidator;

/**
 * @author jianglibo@gmail.com
 *         2015年12月25日
 *
 */
public class RepeatValidator implements PasswordValidateMethod {

    /* (non-Javadoc)
     * @see com.m3958.logcollector.passwordvalidator.PasswordValidateMethod#validate(java.lang.String)
     */
    private final int repeat;
    
    /**
     * 
     */
    public RepeatValidator(int repeat) {
        this.repeat = repeat;
    }
    
    @Override
    public void validate(String password) throws PasswordValidatorException {
        char[] chars = password.toCharArray();
        int maxdp = 0;
        int dp = 0;
        for (int i = 0; i < chars.length - 1; i++) {
            if (chars[i] == chars[i + 1]) {
                dp++;
            } else {
                if (dp > maxdp) {
                    maxdp = dp;
                }
                dp = 0;
            }
        }
        if (dp > maxdp) {
            maxdp = dp;
        }
        if (maxdp >= getDuplication()) {
            throw new PasswordValidatorException("MAX_REPEAT");
        }
        
    }

    public int getDuplication() {
        return repeat;
    }

}
